import * as db from './db.js';
//操作失败的提示信息
function errorToShow(msg = '操作失败', callback = function() {}) {
	uni.showToast({
		title: msg,
		icon: 'none',
		duration: 1500
	})
	setTimeout(function() {
		callback()
	}, 1500)
}

//操作成功后，的提示信息
function successToShow(msg = '保存成功', callback = function() {}) {
	uni.showToast({
		title: msg,
		icon: 'success',
		duration: 1000
	})
	setTimeout(function() {
		callback()
	}, 500)
}
//跳转到登陆页面
function jumpToLogin(method) {
	var now_time = Date.parse(new Date())
	var value = db.get('jump_to_login')
	if (!value) {
		value = 0
	}
	if (now_time - value > 3000) {
		db.set('jump_to_login', now_time); //暂时屏蔽登录时间查询
		uni.showToast({
			title: '请先登录!',
			icon: 'none',
			duration: 1000,
			success: function(res) {
				setTimeout(() => {
					uni.hideToast();
					uni.switchTab({
						url: '/pages/member/member',
						animationType: 'pop-in',
						animationDuration: 200
					})
				}, 500)
			}
		})
	}
}

function throttle(fn, gapTime) {
	if (gapTime == null || gapTime == undefined) {
		gapTime = 1500
	}

	let _lastTime = null

	// 返回新的函数
	return function() {
		let _nowTime = +new Date()
		if (_nowTime - _lastTime > gapTime || !_lastTime) {
			fn.apply(this, arguments) //将this和参数传给原函数
			_lastTime = _nowTime
		}
	}
}

function timestampToTime(timestamp) {
	timestamp=timestamp||Date.now()
	var date = new Date(timestamp);
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
	var D = date.getDate() + ' ';
	var h = date.getHours() + ':';
	var m = date.getMinutes() + ':';
	var s = date.getSeconds();
	return Y + M + D + h + m + s;
}
/**
 * 统一跳转
 */
function navigateTo(url) {
	uni.navigateTo({
		url: url,
		animationType: 'pop-in',
		animationDuration: 300
	})
}
/**
 * 跳转的上一级页面
 */
function navigateBack(delta = 1, callback) {
	uni.navigateBack({
		delta,
		animationDuration: 300,
		success: callback
	})
}
/**
 * 计算百分比
 */
function GetPercent(num, total) {
	num = parseFloat(num);
	total = parseFloat(total);
	if (isNaN(num) || isNaN(total)) {
		return "-";
	}
	return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00) + "%";
}
/**
 *  过滤非法字符
 */
function filterNum(i) {
	let num = i.charAt(i.length - 1)
	var reg = new RegExp("^[0-9]*$")
	if (!reg.test(num)) {
		return i.slice(0, -1)
	} else {
		return i
	}
}
/**
 * 手机验证
 */
export function checkPhone(value) {
	let reg = /^1\d{10}$/
	if (reg.test(value)) {
		return true;
	}
	toast('手机号格式错误');
	return false
}
/**
 * 密码验证
 */
export function checkPwd(value) {
	if (value.length >= 6) {
		return true;
	}
	toast('密码必须大于6位');
	return false
}
/**
 * 验证码验证
 */
export function checkCode(value) {
	if (value.length == 6) {
		return true;
	}
	toast('验证码必须是6位数字');
	return false
}
/**
 * 身份证验证
 */
export function checkIdCard(value) {
	let reg = /^\d{15}|\d{18}$/
	if (reg.test(value)) {
		return true;
	}
	toast('身份证必须是15或18位数字');
	return false
}
/**
 * 银行卡验证
 */
export function checkBankCard(value) {
	let reg = /^([1-9]{1})(\d{14}|\d{15}|\d{16}|\d{18})$/
	if (reg.test(value)) {
		return true;
	}
	toast('请输入正确的银行卡号');
	return false
}
/**
 * 判断男女
 */
function isgender(val) {
	if (val == 0) {
		return '男'
	} else if (val == 1) {
		return '女'
	} else {
		return '保密'
	}
}
/**
 * 关闭当前页面统一跳转
 */
function redirectTo(url, callback) {
	uni.redirectTo({
		url: url,
		animationType: 'pop-in',
		animationDuration: 300,
		success: callback
	})
}
/**
 * 关闭所有的页面打开一个页面
 */
function reLaunch(url, callback) {
	uni.reLaunch({
		url: url,
		animationType: 'pop-in',
		animationDuration: 300,
		success: callback
	})
}
/**
 * tabber跳转
 */
function switchTabTo(url, callback) {
	uni.switchTab({
		url: url,
		animationType: 'pop-in',
		animationDuration: 300,
		success: callback
	})
}
export {
	errorToShow,
	successToShow,
	jumpToLogin,
	throttle,
	timestampToTime,
	navigateTo,
	GetPercent,
	filterNum,
	navigateBack,
	isgender,
	redirectTo,
	reLaunch,
	switchTabTo
}
